var app = getApp()

export default function showGetUser(){
  wx.showModal({
    title: '授权手机号',
    content: '实名认证需要获取您的手机号码',
    success: res => {
      if (res.confirm) {
        getUserProfile()
      }
    }
  })
}

function getUserProfile () {
  wx.getUserProfile({
    desc: '展示用户信息',
    success: (res) => {
      console.log(res)
      onGetOpenid(res.userInfo)
    }
  })
}

function onGetOpenid(res) {
  // 调用云函数
  wx.cloud.callFunction({
    name: 'login',
    data: {
      $url: 'user',
      ava: res.avatarUrl,
      name: res.nickName,
      sex: res.gender
    },
    success: res => {
      console.log(res)
      if (res.result) {
        app.globalData.isLogin = true
        app.globalData.userInfo = res.result
        app.globalData.member = res.result.member
        app.globalData.langNo = res.result.langNo
        app.globalData.openId = res.result.openId
        wx.setStorageSync('userInfo', res.result)
        wx.setStorageSync('isLogin',true)
        wx.showToast({
          title: '登录成功',
          icon: 'success',
          duration: 1000
        })
        this.onLoad()
      }
    },
    fail: err => {
      console.error(err)
    }
  })
}